package com.modestmaps.extras 
{	
	// This class has been modified from its original modestmaps version 1.0
	// to support new PolylineOverlay class.
	// 4/24/08 Jay Wood  / San Diego Union-Tribune
    import com.modestmaps.Map;
    import com.modestmaps.events.MapEvent;
    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Point;

    /** 
     * Serves as the base for subclasses that add vector graphics to your map,
     * extended to support the use of PolylineOverlay and OverlayManager.
     * 
     * <p>Subclass Overlay and override the redraw(sprite:Sprite) method to use it.</p>
	 * <p>This is a modification to the Overlay class in modestmaps mapping API for Actionscript 3
	 * (current trunk version as of 6/19/08). Changes:</p>
     * <ul>
     * <li>simplified display model by removing polySprite and adding overlay directly to polyCont</li>
     * <li>changed polyCont from private to protected, to enable PolylineOverlay to read its positon</li>
     * <li>added a groupID var to enable OverlayManager to operate on groups of related overlays 
     * (show, hide, remove operations)</li>
     * <li>added a visibility property that indicates the current visibility of the overlay...
     * this is used to restore visibility of PolylineOverlay instances after
     * the overlay is temporarily hidden for smoother display during map zoom. </li>
     * </ul>
	 *
	 * @see http://www.modestmaps.com the official ModestMaps site
	 * @see com.modestmaps.OverlayManager OverlayManager
	 * @see PolylineOverlay
     */     
    public class Overlay extends Sprite
    {
        public var map:Map;        
        private var panStart:Point;

        /// jbw -- chanaged from private to public for PolylineOverlay -- 4/24/08
        public var polyCont:Sprite = new Sprite();  

		/** identifies the polygon as part of a group */
		// added 4/24/08 jbw
        public var groupID:String;
        // added 4/24/08 jbw -- stores the persistent visibility state of the overlay
        // during temporary hide-on-zoom for PolylineOverlays
        private var _visibility:Boolean = true;

        public function Overlay(map:Map,groupID:String="")
        {
            this.mouseEnabled = false;
            this.mouseChildren = false; 		 	
            this.map = map;
            this.cacheAsBitmap = true;
			// add group id -- jbw 4/24/08
            this.groupID = groupID;
            addChild(polyCont);
//          polyCont.addChild(polySprite); // jbw -- disabled 4/24/08
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }
                
        private function onAddedToStage(event:Event):void 
        {
            refresh();
            onMapResize(null);
            map.addEventListener(MapEvent.START_PANNING, onMapStartPan);
            map.addEventListener(MapEvent.PANNED, onMapPanned);
            map.addEventListener(MapEvent.STOP_PANNING, onMapStopPan);
            map.addEventListener(MapEvent.ZOOMED_BY, onMapZoomedBy);
            map.addEventListener(MapEvent.STOP_ZOOMING, onEndZoom);
            map.addEventListener(MapEvent.EXTENT_CHANGED, onMapChange);
            map.addEventListener(MapEvent.RESIZED, onMapResize);
        }
        
        private function onMapResize(event:Event):void
        {
            refresh();
        }
        
		public function refresh(...args):void
        {
            polyCont.scaleX = polyCont.scaleY = 1.0;
            polyCont.x = stage.stageWidth/2;
            polyCont.y = stage.stageHeight/2;
//~~            polySprite.x = stage.stageWidth/2; // jbw -- disabled 4/24/08
//~~            polySprite.y = stage.stageHeight/2;
//~~            redraw(polySprite);
			redraw(polyCont);
        }

        /** redraw the overlay onto the given Sprite - override this in your subclass */
        public function redraw(sprite:Sprite):void 
        {
            trace("*** you should subclass Overlay and override redraw rather than using Overlay directly");
        }

       private function onMapZoomedBy(event:MapEvent):void 
        {
            polyCont.scaleX = polyCont.scaleY = Math.pow(2, event.zoomDelta);
            refresh();
        }
        
        private function onEndZoom(event:MapEvent):void
        {
            refresh();
        }

        private function onMapChange(event:MapEvent):void
        {
            refresh();
        }
    
        private function onMapStartPan(event:MapEvent):void
        {
            panStart = new Point(polyCont.x,polyCont.y);
        }

        private function onMapStopPan(event:MapEvent):void
        {
            refresh();
        }
        
        private function onMapPanned(event:MapEvent):void
        {
            if (panStart) {
	            polyCont.x = panStart.x + event.panDelta.x;
    	        polyCont.y = panStart.y + event.panDelta.y;
            }
        }


		//////////// new methods created for PolylineOverlay extensions ////////////////
		//////////// 4/24/08 Jay Wood / San Diego Union-Tribune         ////////////////

		/** returns the value of visibility property.
		 * Note: visibility is not the same as the visible property --
		 * visible may be temporarily set to false, for smoother redraw on zooming,
		 * while visibility denotes the user-selected visual state of the polygon
		 * (used for restoring visible polygons after a hide-on-zoom).
		 */
		public function get visibility():Boolean {return _visibility;}

		/** 
		 * Sets visiblility property to the specified value (true/false), and 
		 * shows or hides the overlay accordingly.
		 */
		public function set visibility(vis:Boolean):void
		{
			_visibility = vis;
			visible = vis;
		}
    }
}